#define _CRT_SECURE_NO_WARNINGS 1
#include"Dlist.h"

LTNode* ListInit()
{
	LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
	phead->next = phead;
	phead->pre = phead;
	return phead;
}
void ListPushBack(LTNode* phead, LTDateType x)
{
	//ListInsert(phead);
	assert(phead);
	LTNode* tail = phead->pre;
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	newnode->data = x;

	tail->next = newnode;
	newnode->pre = tail;
	newnode->next = phead;
	phead->pre = newnode;

}
void ListPrint(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
void ListPopBack(LTNode* phead)
{
	assert(phead->next != phead);
	LTNode* tail = phead->pre;
	(tail->pre)->next = phead;
	phead->pre = tail->pre;
	free(tail);
	tail = NULL;
}

void ListPushFront(LTNode* phead, LTDateType x)
{
	//ListInsert(phead->next);
	assert(phead);
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	newnode->data = x;

	newnode->next = phead->next;
	(phead->next)->pre = newnode;

	newnode->pre = phead;
	phead->next = newnode;
}
void ListPopFront(LTNode* phead)
{
	LTNode* next = phead->next;
	LTNode* Nextnext = next->next;
	Nextnext->pre = phead;
	phead->next = Nextnext;
	free(next);
	next = NULL;
}
LTNode* ListFind(LTNode* phead, LTDateType x)
{
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void ListInsert(LTNode* pos, LTDateType x)
{
	assert(pos);
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	newnode->data = x;
	LTNode* posPre = pos->pre;
	posPre->next = newnode;
	newnode->pre = posPre;
	newnode->next = pos;
	pos->pre = newnode;
}
void ListErase(LTNode* pos)
{
	assert(pos);
	LTNode* Next = pos->next;
	LTNode* Pre = pos->pre;
	Pre->next = Next;
	Next->pre = Pre;
	free(pos);
	pos = NULL;
}

void ListDestory(LTNode* phead)
{
	LTNode* cur = phead->next;
	
	while (cur!=phead)
	{
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
}